/*
 * Copyright 2002-2018 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.web.context;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

/**
 * <p>
 * ContextLoaderListener实现ServletContextListener接口,继承ContextLoader类,实现Servlet容器启动和关闭时,
 * 分别初始化和销毁Root WebApplicationContext容器.
 * </p>
 *
 * @author Juergen Hoeller
 * @author Chris Beams
 * @see #setContextInitializers
 * @see org.springframework.web.WebApplicationInitializer
 * @since 17.02.2003
 */
public class ContextLoaderListener extends ContextLoader implements ServletContextListener {
	/**
	 * 这两个构造方法,是因为父类ContextLoader有这两个构造方法,所以必须实现两个构造方法,才能调用两个父类方法；
	 * 第二个构造方法,可以直接传递一个WebApplicationContext对象,ContextLoaderListener就无需在创建一个新的WebApplicationContext对象
	 */
	public ContextLoaderListener() {
	}

	public ContextLoaderListener(WebApplicationContext context) {
		super(context);
	}

	/**
	 * 调用父类ContextLoader的#initWebApplicationContext(ServletContext servletContext)方法,初始化WebApplicationContext对象
	 */
	@Override
	public void contextInitialized(ServletContextEvent event) {
		initWebApplicationContext(event.getServletContext());
	}

	/**
	 * 销毁WebApplicationContext容器的逻辑
	 */
	@Override
	public void contextDestroyed(ServletContextEvent event) {
		closeWebApplicationContext(event.getServletContext());
		ContextCleanupListener.cleanupAttributes(event.getServletContext());
	}
}
